package www.wen.com.common.lib.data.network

import okhttp3.CacheControl
import okhttp3.Interceptor
import okhttp3.Response
import www.wen.com.common.lib.app.BaseApplication
import www.wen.com.common.lib.util.NetWorkUtils

/**
 * 为get请求添加拦截器
 */
object CacheInterceptorImpl : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        if (!NetWorkUtils.isNetworkAvailable(BaseApplication.CONTEXT)) {
            request = request.newBuilder()
                    .cacheControl(CacheControl.FORCE_CACHE)
                    .build()
        }
        val response = chain.proceed(request)
        if (!NetWorkUtils.isNetworkAvailable(BaseApplication.CONTEXT)) {
            val maxAge = 60 * 60
            response.newBuilder()
                    .removeHeader("Pragma")
                    .header("Cache-Control", "public, max-age=$maxAge")
                    .build()
        }
        else {
            val maxStale = 60 * 60 * 24 * 28 // tolerate 4-weeks stale
            response.newBuilder()
                    .removeHeader("Pragma")
                    .header("Cache-Control", "public, only-if-cached, max-stale=$maxStale")
                    .build()
        }
        return response
    }
}